﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aliyeye.Car.Net.UI.PopupDialog;
using Aliyeye.Car.Net.UI.Components;
using Aliyeye.Car.Net.UI.Common;

namespace Aliyeye.Car.Net.UI.AdminCustomer
{
    public partial class Archives : BaseForm
    {
        ArchivesSvc.SoapHeaderClass sopaHeader = new ArchivesSvc.SoapHeaderClass();
        private int ArchId = 0;
        public Archives()
        {
            InitializeComponent();
        }

        private void Archives_Load(object sender, EventArgs e)
        {
            BindComboBox();
            //this.pager1.PageCurrent = 1;
            //this.pager1.Bind();
        }

        private void BindComboBox()
        {
            DataSet ds = null;
            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            Cursor.Current = Cursors.WaitCursor;
            ServiceManipulator.SafeAsyncExecution<ArchivesSvc.ArchivesServiceSoapClient>(
                proxy => proxy.GetList(sopaHeader),
                result =>
                {
                    if (result.Error == null)
                    {
                        ds = (DataSet)result.Result;
                        comboBox1.DisplayMember = "Name";
                        //comboBox1.ValueMember = "Id";
                        comboBox1.DataSource = ds.Tables[0];
                        comboBox1.SelectedIndex = 0;

                        combox1.DisplayMember = "Name";
                        //combox1.ValueMember = "Id";
                        combox1.DataSource = ds.Tables[0];
                        combox1.SelectedIndex = 0;

                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtname.Text))
            {
                DialogWarning dia = new DialogWarning("档案值不能为空！");
                dia.ShowDialog();
                return;
            }
            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            Cursor.Current = Cursors.WaitCursor;

            //BLL.Archives bll = new BLL.Archives();
            ArchivesSvc.Archives model = new ArchivesSvc.Archives();

            model.Name = comboBox1.Text;
            model.Namevalue = txtname.Text;
            if (ArchId > 0)
            {
                model.Id = ArchId;

                ServiceManipulator.SafeAsyncExecution<ArchivesSvc.ArchivesServiceSoapClient>(
                proxy => proxy.Update(sopaHeader, model),
                result =>
                {
                    if (result.Error == null)
                    {
                        PageCommon.Notify("修改成功！");
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                        ArchId = 0;
                        txtname.Text = "";
                        btnAdd.Text = "添加";
                        this.pager1.Bind();
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
              );

            }
            else
            {
                ServiceManipulator.SafeAsyncExecution<ArchivesSvc.ArchivesServiceSoapClient>(
                proxy => proxy.Add(sopaHeader, model),
                result =>
                {
                    if (result.Error == null)
                    {
                        PageCommon.Notify("添加成功！");
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                        ArchId = 0;
                        txtname.Text = "";
                        btnAdd.Text = "添加";
                        this.pager1.Bind();
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );
            }
        }
        private int dgvBind()
        {
            string _strSql = "";
            string strWhere = " Name='" + combox1.Text + "'";
            PageData pageData = new PageData(this, pager1, overlay);
            pageData.TableName = "Archives";
            pageData.PrimaryKey = "Id";
            pageData.OrderStr = "Id desc";
            pageData.PageIndex = this.pager1.PageCurrent;
            pageData.PageSize = this.pager1.PageSize;
            pageData.QueryCondition = _strSql + strWhere.ToString();
            pageData.QueryFieldName = "*";

            DataSet ds = pageData.QueryDataTable();
            gvdlist.AutoGenerateColumns = false;

            gvdlist.DataSource = this.pager1.bindingSource;
            return pageData.TotalCount;
        }


        private int pager1_EventPaging(Aliyeye.Car.Net.UI.Components.EventPagingArg e)
        {
            return dgvBind();
        }

        private void gvdlist_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (IsANonHeaderButtonCell(e))
            {
                ButtonClicked(e);
            }
        }
        private void ButtonClicked(DataGridViewCellEventArgs e)
        {
            String strButtonContent = gvdlist.Columns[e.ColumnIndex].Name;
            String id = gvdlist.Rows[e.RowIndex].Cells["编号"].Value.ToString();
            String name = gvdlist.Rows[e.RowIndex].Cells["档案类型"].Value.ToString();
            String Namevalue = gvdlist.Rows[e.RowIndex].Cells["档案值"].Value.ToString();

            if (Namevalue.Trim() == "会员积分" && name.Trim() == "付款方式")
            {
                PageCommon.Alert("会员积分不能操作！");
                return;
            }

            ArchId = int.Parse(id);
            if (strButtonContent.Trim().Equals("编辑"))
            {
                comboBox1.Text = name;
                txtname.Text = Namevalue.ToString();

                btnAdd.Text = "保存";
                btnAdd.Refresh();
                btnCancel.Enabled = true;
            }
            else if (strButtonContent.Trim().Equals("删除"))
            {
                //"确定删除？"
                DialogConfirm diaSecc = new DialogConfirm("确定删除此档案值吗？");
                if (diaSecc.ShowDialog() == DialogResult.OK)
                {
                    overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
                    Cursor.Current = Cursors.WaitCursor;
                    ServiceManipulator.SafeAsyncExecution<ArchivesSvc.ArchivesServiceSoapClient>(
                       proxy => proxy.Delete(sopaHeader, ArchId),
                       result =>
                       {
                           if (result.Error == null)
                           {
                               PageCommon.Notify("删除成功！");
                               overlay.FadeOut();Cursor.Current = Cursors.Default;
                               btnAdd.Text = "添加";
                               btnAdd.Refresh();
                               txtname.Text = "";
                               ArchId = 0;
                               this.pager1.Bind();
                           }
                           else
                           {
                               PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                           }
                       },
                       this
                   );
                }
            }
        }
        private bool IsANonHeaderButtonCell(DataGridViewCellEventArgs cellEvent)
        {
            if (gvdlist.Columns[cellEvent.ColumnIndex] is
                 DataGridViewButtonColumn &&
                 cellEvent.RowIndex != -1)
            { return true; }
            else { return (false); }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnAdd.Text = "添加";
            btnAdd.Refresh();
            txtname.Text = "";
            ArchId = 0;
            btnCancel.Enabled = false;
        }

        private void menuBlueButton1_Click(object sender, EventArgs e)
        {
            dgvBind();
        }

        private void combox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(combox1.Text.Trim()))
                dgvBind();
        }

        private void dgvInfo_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (this.gvdlist.Rows.Count != 0)
            {
                for (int i = 0; i < this.gvdlist.Rows.Count; )
                {
                    this.gvdlist.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.PapayaWhip;
                    i += 2;
                }
            }
        }
    }
}
